﻿using SuperX.Common.Object;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace SuperX.Common.Collection
{
    // 
    public abstract class BasicObjCollection<T> : BasicObject, IObjCollection<T>, IObjCollection, IBasicObject, IJsonSerialize, INotifyPropertyChanged, IDisposable, ICloneable, ICollectionNotify where T : IBasicObject
    {
        public event EventHandler<CollectionChangedArgs> ItemAdded;
        public event EventHandler<CollectionChangedArgs> ItemCleared;
        public event EventHandler<CollectionChangedArgs> ItemRemoved;
        public event EventHandler<CollectionChangedArgs> ItemReplaced;

        public abstract List<string> GetKeys();

        public abstract List<T> GetValues();

        public T GetChildObject(string name)
        {
            return this[name];
        }

        IBasicObject IObjCollection.this[string name]
        {
            get
            {
                return this[name];
            }
        }

        // 
        public abstract T this[string name]
        {
            get;
        }

        // 
        List<IBasicObject> IObjCollection.GetValues()
        {
            return this.GetValues().OfType<IBasicObject>().ToList<IBasicObject>();
        }

        // 
        IBasicObject IObjCollection.GetChildObject(string name)
        {
            return this.GetChildObject(name);
        }

        // 
        bool IBasicObject.IsUniqueName(string name, IObjCollection parent)
        {
            return this.IsUniqueName(name, parent);
        }

        // 
        public abstract override bool IsUniqueName(string name, IObjCollection parent);

        /// <summary>
        /// Notifies the item cleared.
        /// </summary>
        public void NotifyItemCleared()
        {
            EventHandler<CollectionChangedArgs> itemCleared = this.ItemCleared;
            if (itemCleared == null)
            {
                return;
            }
            itemCleared(this, new CollectionChangedArgs(this));
        }

        /// <summary>
        /// Notifies the item removed.
        /// </summary>
        /// <param name="item">The item.</param>
        public void NotifyItemRemoved(object item)
        {
            EventHandler<CollectionChangedArgs> itemRemoved = this.ItemRemoved;
            if (itemRemoved == null)
            {
                return;
            }
            itemRemoved(this, new CollectionChangedArgs(item));
        }

        /// <summary>
        /// Notifies the item added.
        /// </summary>
        /// <param name="item">The item.</param>
        public void NotifyItemAdded(object item)
        {
            EventHandler<CollectionChangedArgs> itemAdded = this.ItemAdded;
            if (itemAdded == null)
            {
                return;
            }
            itemAdded(this, new CollectionChangedArgs(item));
        }

        /// <summary>
        /// Notifies the item Replaced.
        /// </summary>
        /// <param name="item">The item.</param> 
        public void NotifyItemReplaced(object item)
        {
            EventHandler<CollectionChangedArgs> itemReplaced = this.ItemReplaced;
            if (itemReplaced == null)
            {
                return;
            }
            itemReplaced(this, new CollectionChangedArgs(item));
        }

        public virtual void Add(object item)
        {
        }

        public virtual void Clear()
        {
        }

        public virtual void Remove(object item)
        {
        }

        public virtual void Replace(object item)
        {
        }
    }
}
